package com.cci.kangdao.controller.xinguanjia;

import com.cci.kangdao.controller.xinguanjia.dto.XinGuanJiaDto;
import com.cci.kangdao.dao.ExternalAuthMapper;
import com.cci.kangdao.dao.UserTDao;
import com.cci.kangdao.dao.model.ExternalAuthInfo;
import com.cci.kangdao.dao.model.UserT;
import com.cci.kangdao.dto.BaseResponse;
import com.cci.kangdao.model.CipHeader;
import com.cci.kangdao.service.WorkOrderTService;
import com.cci.kangdao.utilTool.CollectionUtils;
import com.cci.kangdao.utilTool.ParameterTool;
import com.gexin.fastjson.JSON;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

@Controller
@RequestMapping("xinGuanJia")
public class XinGuanJiaController {
    private Logger log = Logger.getLogger(this.getClass().getName());
    @Resource
    private ExternalAuthMapper externalAuthMapper;
    @Autowired
    private WorkOrderTService workOrderTService;
    @Autowired
    private UserTDao userTDao;

    /**
     * 信管家  获取历史的工单   根据服务站id 和 esn
     * @param stationId
     * @param esn
     * @param headerMap
     * @param response
     * @return
     */

    @RequestMapping(value = "getWorkOrderByStationAndEsn.aspx", method = RequestMethod.POST)
    @ResponseBody
    public BaseResponse getWorkOrderByStationAndEsn(@RequestBody XinGuanJiaDto xinGuanJiaDto,
                                                    @RequestHeader Map<String, String> headerMap,
                                                    HttpServletResponse response) {
        log.info("接口传参为："+xinGuanJiaDto !=null ? xinGuanJiaDto.toString():"null");
        CipHeader header = JSON.parseObject(JSON.toJSONString(headerMap), CipHeader.class);
        log.info( "header信息:" + header);
        if(null == header){
//            ParameterTool.writeResponse(response, 17,"header为空！");
            return BaseResponse.error(17, "header为空");
        }
        if(StringUtils.isBlank(header.getSource()) || StringUtils.isBlank(header.getVersion()) || StringUtils.isBlank(header.getToken())){
//            ParameterTool.writeResponse(response, 17,"header参数为空！");
            return BaseResponse.error(17, "header参数为空");
        }

        ExternalAuthInfo authInfo = externalAuthMapper.getAuthBySource(header.getSource(),header.getVersion());
        if(null == authInfo){
//            ParameterTool.writeResponse(response, 17,"认证信息错误！");
            return BaseResponse.error(17, "认证信息错误");
        }
        if(!authInfo.getToken().equals(header.getToken())){
//            ParameterTool.writeResponse(response, 17,"token错误！");
            return BaseResponse.error(17, "token错误");

        }
        if (xinGuanJiaDto == null || null == xinGuanJiaDto.getUserId() || CollectionUtils.isEmpty(xinGuanJiaDto.getEsnList())) {
            return BaseResponse.error(-99, "参数不能为空");
        }
        UserT userT = userTDao.selectById(xinGuanJiaDto.getUserId());
        if (null == userT) {
            return BaseResponse.error(-99, "userId错误");
        }
        return workOrderTService.getWorkOrderByStationAndEsn(userT.getServiceStationID(), xinGuanJiaDto.getEsnList());
    }
}
